package com.itheima.health.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

public class POIUtils {

    public static List<String[]> readExcel(MultipartFile file) throws IOException {
        List<String[]> dataList = new ArrayList<>();

        InputStream inputStream = file.getInputStream();
        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);

        for (Row row : sheet) {
            String[] rowData = new String[row.getLastCellNum()];
            int i = 0;
            for (Cell cell : row) {
                String cellValue = cell.toString();
                rowData[i++] = cellValue;
            }
            dataList.add(rowData);
        }

        workbook.close();
        inputStream.close();

        return dataList;

    }
}